//turret.txt - Simple script for turrets
//Cell 0,1 - Stuff done flag. If both 0, nothing. Otherwise If non-zero, this 
//turret wont attack. If the turret is itself attacked, will set the not-attack setting back to 0.
//Cell 2,3 - Stuff done flag. If both 0, nothing. Otherwise when this is killed, set to 1.

begincreaturescript;

short i,target;
short mess1 = 0;
short mess2 = 0;
short mess3 = 0;
short last_abil;

body;

beginstate INIT_STATE;
	set_name(ME,"Presence-Fyora");
	change_max_health(ME,50);
	set_boss_level(ME,2);
	place_particle_num(ME,18,12,4);		
	place_particle_num(ME,19,12,4);		
	place_particle_num(ME,20,12,4);		
	set_walk_speed(ME,0);
	
	break;

beginstate DEAD_STATE;
	sf(6,9,1);
	sf(6,24,1);
	sf(2,3,1);
	award_party_xp(200,3);
	begin_talk_mode(28);
break;

beginstate START_STATE; 
	if (who_shot_me() >= 0) {
		if (mess1 == 0) {
			mess1 = 1;
			last_abil = get_current_tick();
			begin_talk_mode(23);
			}

		set_foe_target(ME,who_shot_me());
		do_attack();
		set_state(3);
		}

		
	if (get_foe_target(ME,8,0)) {
		if (mess1 == 0) {
			mess1 = 1;
			last_abil = get_current_tick();
			begin_talk_mode(23);
			}
		do_attack();
		set_state(3);
		}
	if (am_i_doing_action() == FALSE)
		end_combat_turn();
break;

beginstate 3; // attacking
	if (target_ok() == FALSE)
		set_state(START_STATE);
	if (dist_to_char(get_target()) > 8) {
		set_foe_target(ME,-1);
		set_state(START_STATE);
		}

	if ((mess3 > 0) && (gf(6,13) == 2) && (get_char_status(ME,20) == 0))
		force_char_status(ME,20,10);
	
	if ((mess2 > 0) && (can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) < 2)) {
		if (mess3 == 0) {
			mess3 = 1;
			if (gf(6,13) == 0)
				begin_talk_mode(27);
			if (gf(6,13) == 1)
				begin_talk_mode(25);
			if (gf(6,13) == 2) {
				force_char_status(ME,20,10);
				begin_talk_mode(26);
				}
			}
		}
	if ((mess3 > 0) && (gf(6,13) == 0)) {
		if (char_ok(58) == FALSE) {
			print_str_color("The machinery creates a new beetle!",2);
			spawn_creature(50);
			place_particle_num(58,10,7,10);
			set_summon_level(58,1);
			pc_heard_sound_delay(136,100);						
			}
		if (char_ok(59) == FALSE) {
			print_str_color("The machinery creates a new beetle!",2);
			spawn_creature(51);
			place_particle_num(59,10,7,10);
			set_summon_level(59,1);
			pc_heard_sound_delay(136,100);						
			}
			
		}
	if ((can_see_char(get_target())) && (is_combat()) && (tick_difference(last_abil,get_current_tick()) > 1)) {
		if (mess2 == 0) {
			mess2 = 1;
			last_abil = get_current_tick();
			begin_talk_mode(24);
			}
		i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i) == FALSE)
			i = random_group_member(0);
		if (can_see_char(i)) {
			run_char_animation(2,1,35);	
			if (pc_num() == i) {
				print_named_str(ME,"magnifies your confusion!");
				if (get_char_status(i,0) >= 0)
					set_char_status(i,0,-8);
					else set_char_status(i,8,-8);
				shoot_projectile(ME,get_target(),38);
				pc_heard_sound_delay(128,100);						
				}
				else {
					print_named_str(ME,"confuses one of your allies.");
					shoot_projectile(ME,get_target(),38);
					set_char_status(i,17,20);
					pc_heard_sound_delay(124,100);			
					}
			}
		
		last_abil = get_current_tick();
		end();
		}

	do_attack();

	turret_heal();
break;

beginstate TALKING_STATE;
break;